|
|
|
|
|
|
|
background was not blank, the program would need to use the techniques described earlier in this chapter to redraw it. |
|
|
|
|
|
|
|
|
This program can display images much faster than a typical monitor can display them. When it displays more than 30 or so frames per second, the program can cause strange temporal aliasing effects. |
|
|
|
|
|
|
|
|
Because the program is so fast, you can extend it to animate much more complicated scenes involving many objects while still displaying the 20 or so frames per second you need to produce smooth animation. |
|
|
|
|
|
|
|
|
A sprite is a programming abstraction that represents some object in an animation. A sprite should contain all the information needed to describe, update, and draw the object it represents. To make a program showing an animation of bouncing balls, you could make a sprite to represent each ball. The sprites would need to keep track of the position and velocity of the balls they represent. They would also need to be able to move their balls and to draw them when necessary. |
|
|
|
|
|
|
|
|
In Visual Basic, you can build a sprite using a class. You can create a different class for each different kind of sprite your program needs. You might have classes for sprites representing bouncing balls, spinning rectangles, growing and shrinking triangles, and so forth. |
|
|
|
|
|
|
|
|
All sprite classes should provide two key methods: MoveSprite and DrawSprite. The main program can use these methods to make the sprites move and display themselves. The sprite classes should also define whatever data they need to draw their particular objects. |
|
|
|
|
|
|
|
|
To give the main program a consistent way to manage the sprites, create a generic Sprite class. Make this class declare public MoveSprite and DrawSprite methods. This class does not need to implement these routines; it merely defines them as shown in the following code. |
|
|
|
 |
|
|
|
|
' Move the sprite keeping it in bounds. Public Sub MoveSprite( _
ByVal xmin As Integer, ByVal xmax As Integer, _
ByVal ymin As Integer, ByVal ymax As Integer)
End Sub
' Draw the sprite on the PictureBox. Public Sub DrawSprite(pic As PictureBox)
End Sub |
|
|
|
|
|
|
|
|
Now add the statement Implements Sprite to the other sprite classes. That tells Visual Basic that the class implements the MoveSprite and DrawSprite methods defined by the Sprite class. |
|
|
|
|
|